#!/usr/bin/ruby -Ku
# -*- coding: utf-8 -*-
require 'test/unit'
require 'm3g'

class Test_Mesh < Test::Unit::TestCase
  def setup
  end

  def teadown
  end

  def test_defualt_variables_1
    vbuf          = M3G::VertexBuffer.new 
    indices       = [0,1,2]
    strip_lengths = [3]
    ibufs         = [(M3G::TriangleStripArray.new indices, strip_lengths),
                     (M3G::TriangleStripArray.new indices, strip_lengths)]
    apps          = [M3G::Appearance.new, M3G::Appearance.new]
    mesh          = M3G::Mesh.new vbuf, ibufs, apps

    assert_equal vbuf    , mesh.vertex_buffer
    assert_equal 2       , mesh.submesh_count
    assert_equal ibufs[0], mesh.index_buffer[0]
    assert_equal ibufs[1], mesh.index_buffer[1]
    assert_equal apps[0] , mesh.appearance[0]
    assert_equal apps[1] , mesh.appearance[1]
  end

  def test_defualt_variables_2
    vbuf          = M3G::VertexBuffer.new 
    indices       = [0,1,2]
    strip_lengths = [3]
    ibuf          = M3G::TriangleStripArray.new indices, strip_lengths
    app           = M3G::Appearance.new
    mesh          = M3G::Mesh.new vbuf, ibuf, app

    assert_equal vbuf, mesh.vertex_buffer
    assert_equal 1   , mesh.submesh_count
    assert_equal ibuf, mesh.index_buffer[0]
    assert_equal app , mesh.appearance[0]
  end

  def test_set_appearance
    vbuf          = M3G::VertexBuffer.new 
    indices       = [0,1,2]
    strip_lengths = [3]
    ibufs         = [(M3G::TriangleStripArray.new indices, strip_lengths),
                     (M3G::TriangleStripArray.new indices, strip_lengths),
                     (M3G::TriangleStripArray.new indices, strip_lengths)]
    apps          = [M3G::Appearance.new, M3G::Appearance.new, M3G::Appearance.new]
    app4          = M3G::Appearance.new
    mesh          = M3G::Mesh.new vbuf, ibufs, apps


    mesh.appearance[1] = nil
    mesh.appearance[2] = app4

    # 注意: Desktop-M3Gの実装にバグがあって
    # setAppearance (i, NULL)が期待する動作をしない。
    # ここはエラーになる。
    assert_equal apps[0] , mesh.appearance[0]
    assert_equal nil     , mesh.appearance[1]
    assert_equal app4    , mesh.appearance[2]


  end

  def test_duplicate
    vbuf          = M3G::VertexBuffer.new 
    indices       = [0,1,2]
    strip_lengths = [3]
    ibuf          = M3G::TriangleStripArray.new indices, strip_lengths
    app           = M3G::Appearance.new
    mesh1         = M3G::Mesh.new vbuf, ibuf, app

    mesh2 = mesh1.duplicate

    # 注意:duplicateされるのはmesh本体のみ。
    # meshから参照されるvbuf,ibuf,appは複製されず
    # 同じインスタンスを参照する
    assert_not_equal mesh2, mesh1
    assert_equal vbuf, mesh2.vertex_buffer
    assert_equal 1   , mesh2.submesh_count
    assert_equal ibuf, mesh2.index_buffer[0]
    assert_equal app , mesh2.appearance[0]
  end

  def test_references
    vbuf          = M3G::VertexBuffer.new 
    indices       = [0,1,2]
    strip_lengths = [3]
    ibuf          = M3G::TriangleStripArray.new indices, strip_lengths
    app           = M3G::Appearance.new
    mesh          = M3G::Mesh.new vbuf, ibuf, app

    objs = mesh.references
    assert_equal 3, objs.length
    assert_equal vbuf, objs[0]
    assert_equal ibuf, objs[1]
    assert_equal app , objs[2]
  end

end
